﻿
using System;
using System.ServiceModel;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnterpriseServices.DalEf;

namespace EnterpriseServices.Service
{
    public partial class RoleManagerRemotingProxy : IDisposable, IRoleManagerRemoting
    {

        /// <summary>
        /// Provides the singleton pattern.
        /// </summary>
        /// <returns></returns>
        public static RoleManagerRemotingProxy sharedInstance()
        {
            return SHARED_INSTANCE;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public void Initialize()
        {
            if (!initialized)
            {
                timeout.AutoReset = true;
                timeout.Elapsed += new System.Timers.ElapsedEventHandler(channelManager.timeout_Elapsed);
                timeout.Enabled = true;
                initialized = true;
            }
        }

        #region IRoleManagerRemoting Members

        public void AddUsersToRole(string application, string[] userNames, string role)
        {
            throw new NotImplementedException();
        }

        public void AddUsersToRoles(string application, string[] userNames, string[] roles)
        {
            throw new NotImplementedException();
        }

        public void AddUserToRole(string application, string userName, string role)
        {
            throw new NotImplementedException();
        }

        public void AddUserToRoles(string application, string userName, string[] roles)
        {
            throw new NotImplementedException();
        }

        public void CreateRole(string application, string role)
        {
            throw new NotImplementedException();
        }

        public bool DeleteRole(string application, string role, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public void DeleteAllRoles(string application, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public List<EnterpriseServices.DalEf.Role> GetAllRoles(string ApplicationName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a list of the roles that a user is in.
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        public List<Role> GetRolesByUser(string ApplicationName, string UserName)
        {
            Initialize();
            List<Role> roles = channelManager.ChannelSetup(ChannelTask.Open).GetRolesByUser(ApplicationName, UserName);
            return roles;
        }

        public List<User> GetUsersInRole(string ApplicationName, string RoleName)
        {
            throw new NotImplementedException();
        }

        public void RemoveUserFromRole(string application, string userName, string roleName)
        {
            throw new NotImplementedException();
        }

        public void RemoveUsersFromRole(string application, string[] users, string role)
        {
            throw new NotImplementedException();
        }

        public void RemoveUserFromRoles(string application, string user, string[] roles)
        {
            throw new NotImplementedException();
        }

        public void RemoveUsersFromRoles(string application, string[] users, string[] roles)
        {
            throw new NotImplementedException();
        }

        bool IRoleManagerRemoting.IsRolesEnabled(string application)
        {
            throw new NotImplementedException();
        }

        bool IRoleManagerRemoting.RoleExists(string application, string role)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            timeout.Enabled = false;
            channelManager.ChannelSetup(ChannelTask.Close);
        }

        #endregion
        
        #region Variables declaration

        private static RoleManagerRemotingProxy SHARED_INSTANCE = new RoleManagerRemotingProxy();

        bool initialized = false;
        const string endpointName = "IRoleManagerRemoting.Endpoint";
        ChannelManager<IRoleManagerRemoting> channelManager = new ChannelManager<IRoleManagerRemoting>(endpointName);
        System.Timers.Timer timeout = new System.Timers.Timer(60000); // this should not below 1000, else there is no benefit

        #endregion
    }
}
